package com.tjsinfo.tjsoa.address.common;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import org.hibernate.Session;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.Order;


/**
 * A data access object (DAO) providing persistence and search support for
 * TAddresslistCommon entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see com.tjsinfo.oa.addresslist.common.TAddresslistCommon
 * @author MyEclipse Persistence Tools
 */

public class TAddresslistCommonDao extends HibernateDaoSupport implements ITAddresslistCommonDao {
	
	private static final Log log = LogFactory
			.getLog(TAddresslistCommonDao.class);
	public void save(TAddresslistCommon transientInstance) {
		log.debug("saving TAddresslistCommon instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(TAddresslistCommon persistentInstance) {
		log.debug("deleting TAddresslistCommon instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}
	
	public void delete(String propertyName, Object value) {
		log.debug("deleting TAddresslistCommon instance");
		try {
			String queryString = "delete TAddresslistCommon where "+propertyName+" = "+value;
			getHibernateTemplate().delete(value);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
		
	}
	public void update(TAddresslistCommon transientInstance) {
		log.debug("saving TAddresslistCommon instance");
		try {
			this.getHibernateTemplate().update(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}
	public TAddresslistCommon findById(java.lang.Integer id) {
		log.debug("getting TAddresslistCommon instance with id: " + id);
		try {
			TAddresslistCommon instance = (TAddresslistCommon) getHibernateTemplate()
					.get("com.tjsinfo.tjsoa.address.common.TAddresslistCommon", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
		
	public List findByExample(TAddresslistCommon instance) {
		log.debug("finding TAddresslistCommon instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding TAddresslistCommon instance with property: "
				+ propertyName + ", value: " + value);
		try {
			String queryString = "from TAddresslistCommon as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findAll() {
		log.debug("finding all TAddresslistCommon instances");
		try {
			String queryString = "from TAddresslistCommon order by Alt asc";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
	public List findByLikePropertyAndProperty(String chineseName, Object value, String depatment,Object values) {
		Session session=getSession();
		log.debug("finding TAddresslistCommon instance with property: "
				+ chineseName + ", value: " + value + ", property:" + depatment +",value:"+values);
		try {
			Criteria criteria = session.createCriteria(TAddresslistCommon.class);
			criteria.add(Expression.like(chineseName, value));
			criteria.add(Expression.like(depatment, values));
			criteria.addOrder(Order.asc("alt"));
			return criteria.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

}